package com.example.coapdemo.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.MediaTypeRegistry;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class CoapDemoClient {
    public static void main(String[] args) throws JsonProcessingException {
        CoapDemoClient coapDemoClient = new CoapDemoClient();
        // coapDemoClient.testPostCoAP();
        coapDemoClient.testGetCoAP();

        // coapDemoClient.testPostCbor();
    }


    /**
     * 测试Coap的客户端
     */
    private void testGetCoAP() {
        String coapUrl = "coap://127.0.0.1:5683/mqtt?name=Damon&age=18"; // CoAP服务器的地址和端口以及资源路径
        // String coapUrl = "coap://39.99.85.165:5683/mqtt"; // CoAP服务器的地址和端口以及资源路径
        CoapClient client = new CoapClient(coapUrl);
        CoapResponse response = client.get();
        if (response != null) {
            System.out.println(response.getCode()); // 打印响应码
            System.out.println(response.getResponseText()); // 打印响应内容
        } else {
            System.out.println("No response received.");
        }
        client.shutdown();
    }


    /**
     * 测试Coap的客户端
     */
    private void testPostCoAP() throws JsonProcessingException {
        String coapUrl = "coap://127.0.0.1:5683/mqtt"; // CoAP服务器的地址和端口以及资源路径
        CoapClient client = new CoapClient(coapUrl);
        String data = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        byte[] cborData = new ObjectMapper(new CBORFactory()).writeValueAsBytes(new ObjectMapper().readTree(data));

        System.out.println(Arrays.toString(cborData));
        System.out.println(Arrays.toString(data.getBytes(StandardCharsets.UTF_8)));
        // CoapResponse response = client.post(cborData, MediaTypeRegistry.APPLICATION_CBOR);
        CoapResponse response = client.post(data, MediaTypeRegistry.APPLICATION_JSON);

        if (response != null) {
            System.out.println(response.getCode()); // 打印响应码
            System.out.println(response.getResponseText()); // 打印响应内容
        } else {
            System.out.println("No response received.");
        }
        client.shutdown();
    }


    /**
     * 测试Coap的客户端
     */
    private void testPostCbor() throws JsonProcessingException {
        // CoAP服务器的地址和端口以及资源路径
        String coapUrl = "coap://127.0.0.1:5683/cbor";
        CoapClient client = new CoapClient(coapUrl);
        // 将json数据转换为cbor格式 json
        String data = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        byte[] cborData = new ObjectMapper(new CBORFactory()).writeValueAsBytes(new ObjectMapper().readTree(data));

        System.out.println(String.format("cbor bytes len=%s，data =%s", cborData.length, Arrays.toString(cborData)));
        System.out.println(String.format("json bytes len=%s，data =%s", data.getBytes(StandardCharsets.UTF_8).length, Arrays.toString(data.getBytes(StandardCharsets.UTF_8))));
        CoapResponse response = client.post(cborData, MediaTypeRegistry.APPLICATION_CBOR);
        // CoapResponse response = client.post(data, MediaTypeRegistry.APPLICATION_JSON);

        if (response != null) {
            System.out.println(response.getCode()); // 打印响应码
            System.out.println(response.getResponseText()); // 打印响应内容
        } else {
            System.out.println("No response received.");
        }
        client.shutdown();
    }


}

